package com.erp.module.sheJiZhiNan

import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.AdapterView
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.GonggaoTextActivity
import com.erp.bean.AnZhuangZhiNanBean
import com.erp.contacts.Contacts
import com.erp.myAdapter.AnZhuangZhiNanAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_anzhuangzhinan.*
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/1/20/020.
 * 作者：刘洋
 * 设计师指南
 */
class FragmentSheJiZhiNan : BaseKFragment(), AdapterView.OnItemClickListener, View.OnClickListener, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener {
    private val f = "FragmentSheJiZhiNan"
    override fun getLayoutId(): Int {
        fragmentname=f
        return R.layout.fragment_shejizhinan
    }
    var page = 0
    var pagesize = 20
    var sousuo =""
    var code =0
    var updatedialog: AlertDialog? = null
    lateinit var zhinanlist : MutableList<AnZhuangZhiNanBean>
    lateinit var adapter : AnZhuangZhiNanAdapter
    override fun initData() {
        zhinanlist=ArrayList()
        adapter= AnZhuangZhiNanAdapter(activity!!, zhinanlist)
        ZhiNanLv.adapter=adapter
        ZhiNanLv.onItemClickListener=this
        SouSuoIv.setOnClickListener(this)
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
//        updatedialog = Myutil.showupdatedialog(activity, "正在获取数据", false)
        code=1
        Pull.headerRefreshing()
        ZhiNanEt.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    code=0
                    sousuo=ZhiNanEt.text.toString()
                    page = 0
                    pagesize = 20
                    updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
                    httpdata()
                }
                else -> isOK = false
            }
            isOK
        }
    }

    fun qingkong(){
        Pull.headerRefreshing()
    }

    private fun httpdata() {
        Httputil.getsheji(activity, page, pagesize, sousuo, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Log.d("TAAAG","data="+Myutil.decodeUnicode(data))
                if(updatedialog!=null) {
                    updatedialog?.dismiss()
                }
                if (data == "[]") {
                    Myutil.MyTost("已经是全部数据")
                }
                if(code==1){
                    zhinanlist.clear()
                    Pull.onHeaderRefreshComplete()
                }else if(code==2){
                    Pull.onFooterRefreshComplete()
                }else if(code==0){
                    zhinanlist.clear()
                }
                jsondata(data)
                adapter.notifyDataSetChanged()
            }
            override fun onNot(throwable: String) {
                Myutil.MyTost("获取失败")
                if (Myutil.onerror(throwable, activity)) {
                    Myutil.MyTost("已经退出登录")
                }
                if (throwable.indexOf("UnknownHostException") != -1) {
                    Myutil.MyTost("网络异常，点击重新加载")
                }
                if(updatedialog!=null) {
                    updatedialog?.dismiss()
                }
                if(code==1){
                    Pull.onFooterRefreshComplete()
                }else if(code==2){
                    Pull.onHeaderRefreshComplete()
                }
            }
        })
    }
    private fun jsondata(data: String) = try {
        val js = JSONArray(data)
        for (i in 0 until js.length()) {
            val anzhuang = AnZhuangZhiNanBean()
            val jo = js.get(i) as JSONObject
            anzhuang.title=JsonUtil.getvolue(jo,"title")
            anzhuang.time=JsonUtil.getvolue(jo,"ctime")
            anzhuang.id=JsonUtil.getvolue(jo,"id")
            zhinanlist.add(anzhuang)
        }
    } catch (e: JSONException) {
        e.printStackTrace()
    }
    override fun onHeaderRefresh(view: PullToRefreshView?) {
      code=1
        page = 0
        pagesize = 20
        httpdata()
    }

    override fun onFooterRefresh(view: PullToRefreshView?) {
        code=2
        page ++
        pagesize = 20
        httpdata()
    }

    override fun onClick(v: View?) {
        when(v){
            SouSuoIv -> {
                code=0
                sousuo=ZhiNanEt.text.toString()
                page = 0
                pagesize = 20
                updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
                httpdata()
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when(parent){
            ZhiNanLv -> {
                val intent = Intent(activity, GonggaoTextActivity::class.java)
                intent.putExtra("url", Contacts.ZHINAN + zhinanlist[position].id+ "/detail?mobile=1" )
                intent.putExtra("title",zhinanlist[position].title)
                startActivity(intent)
            }
        }
    }
}